# improvement
(q.vars.gc$Factor$qv - q.vars$Factor$qv)/q.vars$Factor$qv
temp_data <- data.frame(x = dturban.gc$agriculture, y = dturban.gc$GC_agriculture)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='blue', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# plot GC
library(ggplot2)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='blue', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
temp_data <- data.frame(x = dturban.gc$industry, y = dturban.gc$GC_industry)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='blue', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# gd
library(GD)
temp_data <- data.frame(x = dturban.gc$service, y = dturban.gc$GC_service)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='blue', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# gd
library(GD)
temp_data <- data.frame(x = dturban.gc$retailsales, y = dturban.gc$GC_retailsales)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='blue', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# gd
library(GD)
temp_data <- data.frame(x = dturban.gc$publicbudget, y = dturban.gc$GC_publicbudget)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='blue', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# gd
library(GD)
temp_data <- data.frame(x = dturban.gc$agriculture, y = dturban.gc$GC_agriculture)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='blue', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# gd
library(GD)
wt <- sdsfun::spdep_contiguity_swm(sf_data, k = 15)
vars.gc <- geocd_vector(sf.data, wt, method = "moran", normalize = TRUE)
dturban.gc <- cbind(dturban, vars.gc[,1:5])
dturban.gc <- dturban.gc[, 1:14]
# plot GC
library(ggplot2)
temp_data <- data.frame(x = dturban.gc$agriculture, y = dturban.gc$GC_agriculture)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='blue', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
wt <- sdsfun::spdep_contiguity_swm(sf_data, k = 8)
vars.gc <- geocd_vector(sf.data, wt, method = "moran", normalize = TRUE)
dturban.gc <- cbind(dturban, vars.gc[,1:5])
dturban.gc <- dturban.gc[, 1:14]
# plot GC
library(ggplot2)
temp_data <- data.frame(x = dturban.gc$agriculture, y = dturban.gc$GC_agriculture)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='blue', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='darkgray', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
d1 <- dturban[, c(2,3,5:9)]
sf.data <- st_as_sf(d1, coords = c("lon", "lat"), crs = 4326)
wt <- sdsfun::spdep_contiguity_swm(sf_data, k = 8)
vars.gc <- geocd_vector(sf.data, wt, method = "moran", normalize = TRUE)
dturban.gc <- cbind(dturban, vars.gc[,1:5])
dturban.gc <- dturban.gc[, 1:14]
# plot GC
library(ggplot2)
temp_data <- data.frame(x = dturban.gc$agriculture, y = dturban.gc$GC_agriculture)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='darkgray', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
d1 <- dturban[, c(2,3,5:9)]
sf.data <- st_as_sf(d1, coords = c("lon", "lat"), crs = 4326)
wt <- sdsfun::spdep_contiguity_swm(sf_data, k = 10)
vars.gc <- geocd_vector(sf.data, wt, method = "moran", normalize = TRUE)
d1 <- dturban[, c(2,3,5:9)]
sf.data <- st_as_sf(d1, coords = c("lon", "lat"), crs = 4326)
wt <- sdsfun::spdep_contiguity_swm(sf_data, k = 9)
vars.gc <- geocd_vector(sf.data, wt, method = "moran", normalize = TRUE)
dturban.gc <- cbind(dturban, vars.gc[,1:5])
dturban.gc <- dturban.gc[, 1:14]
# plot GC
library(ggplot2)
temp_data <- data.frame(x = dturban.gc$agriculture, y = dturban.gc$GC_agriculture)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='darkgray', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# gd
library(GD)
dt.gd <- dturban.gc[, 4:14]
discmethod <- c("equal", "quantile", "natural")
discitv <- 4:5
contvar <- names(dt.gd)[-1]
gd.result <- gdm(formula = urbanindex ~ .,
continuous_variable = contvar,
data = dt.gd,
discmethod = discmethod,
discitv = discitv)
gd.result
plot(gd.result)
plot(gd.result)
plot(gd.result)
# GOZH for all variables
library(GD)
library(rpart)
library(rpart.plot)
# GOZH for five variables
dt.vars <- dturban[, 4:9]
tree <- rpart(urbanindex ~., data = dt.vars)
dt.vars$tree <- as.character(as.numeric(tree$where))
q.vars <- gd(urbanindex ~ tree, dt.vars)
q.vars
rpart.plot(tree, digits = 3, box.palette="GnYlRd")
# GOZH for variables and their geocomplexity patterns
dt.vars.gc <- dturban.gc[, 4:14]
tree <- rpart(urbanindex ~., data = dt.vars.gc)
dt.vars.gc$tree <- as.character(as.numeric(tree$where))
q.vars.gc <- gd(urbanindex ~ tree, dt.vars.gc)
q.vars.gc
rpart.plot(tree, digits = 3, box.palette="GnYlRd")
# improvement
(q.vars.gc$Factor$qv - q.vars$Factor$qv)/q.vars$Factor$qv
wt <- sdsfun::spdep_contiguity_swm(sf_data, k = 7)
vars.gc <- geocd_vector(sf.data, wt, method = "moran", normalize = TRUE)
dturban.gc <- cbind(dturban, vars.gc[,1:5])
dturban.gc <- dturban.gc[, 1:14]
# plot GC
library(ggplot2)
temp_data <- data.frame(x = dturban.gc$agriculture, y = dturban.gc$GC_agriculture)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='darkgray', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# gd
library(GD)
dt.gd <- dturban.gc[, 4:14]
discmethod <- c("equal", "quantile", "natural")
discitv <- 4:5
contvar <- names(dt.gd)[-1]
gd.result <- gdm(formula = urbanindex ~ .,
continuous_variable = contvar,
data = dt.gd,
discmethod = discmethod,
discitv = discitv)
gd.result
plot(gd.result)
# GOZH for all variables
library(GD)
library(rpart)
library(rpart.plot)
# GOZH for five variables
dt.vars <- dturban[, 4:9]
tree <- rpart(urbanindex ~., data = dt.vars)
dt.vars$tree <- as.character(as.numeric(tree$where))
q.vars <- gd(urbanindex ~ tree, dt.vars)
q.vars
rpart.plot(tree, digits = 3, box.palette="GnYlRd")
# GOZH for variables and their geocomplexity patterns
dt.vars.gc <- dturban.gc[, 4:14]
tree <- rpart(urbanindex ~., data = dt.vars.gc)
dt.vars.gc$tree <- as.character(as.numeric(tree$where))
q.vars.gc <- gd(urbanindex ~ tree, dt.vars.gc)
q.vars.gc
rpart.plot(tree, digits = 3, box.palette="GnYlRd")
# improvement
(q.vars.gc$Factor$qv - q.vars$Factor$qv)/q.vars$Factor$qv
wt <- sdsfun::spdep_contiguity_swm(sf_data, k = 15)
vars.gc <- geocd_vector(sf.data, wt, method = "moran", normalize = TRUE)
dturban.gc <- cbind(dturban, vars.gc[,1:5])
dturban.gc <- dturban.gc[, 1:14]
# plot GC
library(ggplot2)
temp_data <- data.frame(x = dturban.gc$agriculture, y = dturban.gc$GC_agriculture)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='darkgray', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# gd
library(GD)
dt.gd <- dturban.gc[, 4:14]
discmethod <- c("equal", "quantile", "natural")
discitv <- 4:5
contvar <- names(dt.gd)[-1]
gd.result <- gdm(formula = urbanindex ~ .,
continuous_variable = contvar,
data = dt.gd,
discmethod = discmethod,
discitv = discitv)
gd.result
plot(gd.result)
# GOZH for all variables
library(GD)
library(rpart)
library(rpart.plot)
# GOZH for five variables
dt.vars <- dturban[, 4:9]
tree <- rpart(urbanindex ~., data = dt.vars)
dt.vars$tree <- as.character(as.numeric(tree$where))
q.vars <- gd(urbanindex ~ tree, dt.vars)
q.vars
rpart.plot(tree, digits = 3, box.palette="GnYlRd")
# GOZH for variables and their geocomplexity patterns
dt.vars.gc <- dturban.gc[, 4:14]
tree <- rpart(urbanindex ~., data = dt.vars.gc)
dt.vars.gc$tree <- as.character(as.numeric(tree$where))
q.vars.gc <- gd(urbanindex ~ tree, dt.vars.gc)
q.vars.gc
rpart.plot(tree, digits = 3, box.palette="GnYlRd")
# improvement
(q.vars.gc$Factor$qv - q.vars$Factor$qv)/q.vars$Factor$qv
d1 <- dturban[, c(2,3,5:9)]
sf.data <- st_as_sf(d1, coords = c("lon", "lat"), crs = 4326)
wt <- sdsfun::spdep_contiguity_swm(sf_data, k = 44)
vars.gc <- geocd_vector(sf.data, wt, method = "moran", normalize = TRUE)
dturban.gc <- cbind(dturban, vars.gc[,1:5])
dturban.gc <- dturban.gc[, 1:14]
# plot GC
library(ggplot2)
temp_data <- data.frame(x = dturban.gc$agriculture, y = dturban.gc$GC_agriculture)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='darkgray', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# gd
library(GD)
dt.gd <- dturban.gc[, 4:14]
discmethod <- c("equal", "quantile", "natural")
discitv <- 4:5
contvar <- names(dt.gd)[-1]
gd.result <- gdm(formula = urbanindex ~ .,
continuous_variable = contvar,
data = dt.gd,
discmethod = discmethod,
discitv = discitv)
gd.result
plot(gd.result)
# GOZH for all variables
library(GD)
library(rpart)
library(rpart.plot)
# GOZH for five variables
dt.vars <- dturban[, 4:9]
tree <- rpart(urbanindex ~., data = dt.vars)
dt.vars$tree <- as.character(as.numeric(tree$where))
q.vars <- gd(urbanindex ~ tree, dt.vars)
q.vars
rpart.plot(tree, digits = 3, box.palette="GnYlRd")
# GOZH for variables and their geocomplexity patterns
dt.vars.gc <- dturban.gc[, 4:14]
tree <- rpart(urbanindex ~., data = dt.vars.gc)
dt.vars.gc$tree <- as.character(as.numeric(tree$where))
q.vars.gc <- gd(urbanindex ~ tree, dt.vars.gc)
q.vars.gc
rpart.plot(tree, digits = 3, box.palette="GnYlRd")
# improvement
(q.vars.gc$Factor$qv - q.vars$Factor$qv)/q.vars$Factor$qv
441*5%
temp_data <- data.frame(x = dturban.gc$retailsales, y = dturban.gc$GC_retailsales)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='darkgray', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
temp_data <- data.frame(x = dturban.gc$agriculture, y = dturban.gc$GC_agriculture)
ggplot(data=temp_data, aes(x=x, y=y)) +
geom_point(color='darkgray', size = 0.5) +
geom_smooth(method = "gam", formula = y ~s(x)) +
theme_bw()
# gd
library(GD)
dt.gd <- dturban.gc[, 4:14]
discmethod <- c("equal", "quantile", "natural")
discitv <- 4:5
contvar <- names(dt.gd)[-1]
gd.result <- gdm(formula = urbanindex ~ .,
continuous_variable = contvar,
data = dt.gd,
discmethod = discmethod,
discitv = discitv)
gd.result
plot(gd.result)
gd.result.interaction <- gd.result$Interaction.detector$Interaction
View(gd.result.interaction)
View(dturban.gc)
setwd(''/Users/268222h/Dropbox/work/reimbursement and travel/202601 Qingdao/teaching/Topic 4/geocomplexity analysis'')
setwd('/Users/268222h/Dropbox/work/reimbursement and travel/202601 Qingdao/teaching/Topic 4/geocomplexity analysis')
dturban <- read.csv("dturban.csv")
# 1) Read shapefile
sa <- st_read("studyarea.shp", quiet = TRUE)
#### create maps of vars and vars.gc
# Packages
library(sf)
library(dplyr)
library(ggplot2)
library(tidyr)
# 1) Read shapefile
sa <- st_read("studyarea.shp", quiet = TRUE)
# 2) Make sure PID types match (common source of join issues)
sa <- sa %>% mutate(PID = as.character(PID))
dturban.gc <- dturban.gc %>% mutate(PID = as.character(PID))
# 3) Join attributes to polygons
sa2 <- sa %>%
left_join(dturban.gc %>% select(PID, agriculture, GC_agriculture), by = "PID")
# 4) Plot two maps (facets)
plot_df <- sa2 %>%
st_drop_geometry() %>%
bind_cols(st_coordinates(st_centroid(sa2)) %>% as.data.frame()) # not necessary; ignore if errors
View(sa2)
# Better: pivot directly on sf object
sa_long <- sa2 %>%
pivot_longer(cols = c(agriculture, GC_agriculture),
names_to = "variable", values_to = "value")
ggplot(sa_long) +
geom_sf(aes(fill = value), color = NA) +
facet_wrap(~ variable, ncol = 2) +
coord_sf(datum = NA) +
theme_minimal(base_size = 12) +
theme(
panel.grid.major = element_blank(),
axis.text = element_blank(),
axis.title = element_blank()
) +
labs(fill = "Value")
# Read + join
sa <- st_read("studyarea.shp", quiet = TRUE) %>%
mutate(PID = as.character(PID))
dturban.gc <- dturban.gc %>%
mutate(PID = as.character(PID))
sa2 <- sa %>%
left_join(dturban.gc %>% select(PID, agriculture, GC_agriculture), by = "PID")
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = NA) +
coord_sf(datum = NA) +
scale_fill_distiller(
palette = "YlGn",
direction = 1,
na.value = "grey90",
labels = scales::label_number(accuracy = 0.01)
) +
theme_minimal(base_size = 12)
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = NA) +
coord_sf(datum = NA) +
scale_fill_distiller(
palette = "YlGn",
direction = 1,
na.value = "grey90",
labels = scales::label_number(accuracy = 0.01)
) +
theme_bw(base_size = 12)
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = NA) +
coord_sf(datum = NA) +
scale_fill_distiller(
palette = "YlGn",
direction = 1,
na.value = "grey90",
labels = scales::label_number(accuracy = 0.01)
) +
theme_bw()
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = NA) +
scale_fill_distiller(
palette = "YlGn",
direction = 1,
na.value = "grey90",
labels = scales::label_number(accuracy = 0.01)
) +
theme_bw()
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = NA) +
scale_fill_distiller(
palette = "YlGn",
direction = 1,
labels = scales::label_number(accuracy = 0.01)
) +
theme_bw()
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = NA) +
scale_fill_distiller(
palette = "YlGn",
direction = 1,
na.value = "grey90",
labels = scales::label_number(accuracy = 0.01)
) +
theme_bw()
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = NA) +
scale_fill_distiller(
palette = "YlGn",
direction = 1,
labels = scales::label_number(accuracy = 0.01)
) +
theme_bw()
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = NA) +
scale_fill_distiller(
palette = "YlGn",
direction = 1
) +
theme_bw()
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = NA) +
scale_fill_distiller(
palette = "YlGn"
) +
theme_bw()
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture)) +
scale_fill_distiller(palette = "YlGn", direction = 1) +
theme_bw()
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = 'lightgray') +
scale_fill_distiller(palette = "YlGn", direction = 1) +
theme_bw()
# --- Map 1: agriculture (YlGn) ---
ggplot(sa2) +
geom_sf(aes(fill = agriculture), color = 'gray') +
scale_fill_distiller(palette = "YlGn", direction = 1) +
theme_bw()
# --- Map 2: GC_agriculture (magma) ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_distiller(palette = "magma", direction = 1) +
theme_bw()
# --- Map 2: GC_agriculture (magma) ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_distiller(palette = "magma") +
theme_bw()
# --- Map 2: GC_agriculture (magma) ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_distiller(palette = "cividis") +
theme_bw()
# --- Map 2: GC_agriculture (magma) ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_viridis_c(option = "cividis") +
theme_bw()
# --- Map 2: GC_agriculture (magma) ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_viridis_c(option = "magma") +
theme_bw()
# --- Map 2: GC_agriculture (magma) ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_viridis_c(option = "magma", direction = 1) +
theme_bw()
# --- Map 2: GC_agriculture (magma) ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_viridis_c(option = "magma", direction = -1) +
theme_bw()
# --- Map 2: GC_agriculture (magma) ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture^2), color = 'gray') +
scale_fill_viridis_c(option = "magma", direction = -1) +
theme_bw()
# --- Map 2: GC_agriculture ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_viridis_c(option = "cividis", direction = -1) +
theme_bw()
# --- Map 2: GC_agriculture ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_viridis_c(option = "plasma", direction = -1) +
theme_bw()
# --- Map 2: GC_agriculture ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_viridis_c(option = "plasma") +
theme_bw()
# --- Map 2: GC_agriculture ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_viridis_c(option = "inferno") +
theme_bw()
# --- Map 2: GC_agriculture ---
ggplot(sa2) +
geom_sf(aes(fill = GC_agriculture), color = 'gray') +
scale_fill_viridis_c(option = "turbo") +
theme_bw()
